//# sourceURL=http://uMapApp/Module/InspectionModule/InspectionModule
define([
    "mhjs/mhjs",
    "uMap/uMap",
    "uEarth/uEarth",
    "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../Road/StakeNumPanel"
    , "./SampledPositionViewport"
    , "../StakeNum/Mileage"
], function (
    mhjs,
    uMap,
    uEarth,
    Module,
    FloatPanel
    , StakeNumPanel
    , SampledPositionViewport
    , Mileage
) {

    function InspectionModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "虚拟巡检";
        _this.icon = _this.app.icons.inspection;

        _this.panels = [];
        _this.items = {};

        //整体功能按钮
        _this.globeButtonPanel = new FloatPanel({"class": "globeButtonPanel InspectionModule"});
        _this.globeButtonPanel.label = new mhjs.Div({"class": "label"});
        _this.globeButtonPanel.label.text("设置");

        _this.globeButtonPanel.directSelect = new mhjs.Select({"class": "select"});
        _this.globeButtonPanel.directSelect.setList([
            {
                "text" : "上行",
                "value": 1
            }, {
                "text" : "下行",
                "value": -1
            }
        ]);

        _this.globeButtonPanel.directSelect.on("select", function (select) {

            _this.sampledPositionViewport.direction = Number(select.value);

            if (Number(select.value) === 1) {
                _this.stakeNumPanel.direction = 0;
            } else {
                _this.stakeNumPanel.direction = 180;
            }

            _this.sampledPositionViewport.updateCameraNow();

        });


        _this.globeButtonPanel.speedSelect = new mhjs.Select({"class": "select"});
        _this.globeButtonPanel.speedSelect.setList([
            {
                "text" : "步行（5米/秒）",
                "value": 5
            }, {
                "text" : "驾车（60公里/小时）",
                "value": 60 * 1000 / 3600
            }, {
                "text" : "驾车（120公里/小时）",
                "value": 120 * 1000 / 3600
            }
        ]);
        _this.globeButtonPanel.speedSelect.select(2);

        _this.globeButtonPanel.speedSelect.on("select", function (select) {
            _this.sampledPositionViewport.speed = select.value;
        });

        _this.globeButtonPanel.heightSelect = new mhjs.Select({"class": "select"});
        _this.globeButtonPanel.heightSelect.setList([
            {
                "text" : "近地",
                "value": 50
            }, {
                "text" : "低空",
                "value": 100
            }, {
                "text" : "高空",
                "value": 200
            }
        ]);
        _this.globeButtonPanel.heightSelect.select(2);

        _this.globeButtonPanel.heightSelect.on("select", function (select) {
            _this.sampledPositionViewport.height = select.value;
            _this.sampledPositionViewport.updateCameraNow();
        });


        // _this.globeButtonPanel.layerButton = new mhjs.Div({"class": "button"});
        // _this.globeButtonPanel.layerButton.text("图层");

        // _this.globeButtonPanel.layerButton.on("click", function () {
        //     _this.app.modules.road.leftPanel.toggle(false);
        // });

        _this.globeButtonPanel
            .append(_this.globeButtonPanel.label)
            .append(_this.globeButtonPanel.directSelect)
            .append(_this.globeButtonPanel.speedSelect)
            .append(_this.globeButtonPanel.heightSelect)
        // .append(_this.globeButtonPanel.layerButton)
        ;

        _this.app.ui.layout.top.append(_this.globeButtonPanel);
        _this.panels.push(_this.globeButtonPanel);


        //整体功能按钮
        _this.playPanel = new FloatPanel({"class": "playPanel InspectionModule"});
        _this.playPanel.backwardButton = new mhjs.Div({"class": "backward button"});
        _this.playPanel.playButton = new mhjs.Div({"class": "play button"});
        _this.playPanel.forwardButton = new mhjs.Div({"class": "forward button"});

        _this.playPanel
            .append(_this.playPanel.backwardButton)
            .append(_this.playPanel.playButton)
            .append(_this.playPanel.forwardButton)
        ;

        _this.playPanel.playButton.on("click", function (button) {

            _this.app.modules.road.leftPanel.min(false);

            if (!_this.sampledPositionViewport.isPlaying) {
                var currentViewport = _this.app.earth.getCameraViewport();

                if (currentViewport) {
                    _this.sampledPositionViewport.pitch = -currentViewport.hpr.pitch;
                    // _this.sampledPositionViewport.height = currentViewport.hpr.range * Math.sin(uEarth.Math.toRadians(-currentViewport.hpr.pitch));
                }
            }

            _this.sampledPositionViewport.toggle();

        });

        _this.playPanel.backwardButton.on("click", function (button) {

            if (_this.sampledPositionViewport.direction === 1) {
                _this.sampledPositionViewport.forward();
            } else {
                _this.sampledPositionViewport.backward();
            }
            _this.sampledPositionViewport.updateCameraNow();
        });

        _this.playPanel.forwardButton.on("click", function (button) {

            if (_this.sampledPositionViewport.direction === -1) {
                _this.sampledPositionViewport.forward();
            } else {
                _this.sampledPositionViewport.backward();
            }
            _this.sampledPositionViewport.updateCameraNow();
        });

        _this.app.ui.layout.bottom.append(_this.playPanel);
        _this.panels.push(_this.playPanel);

        //桩号导航
        _this.stakeNumPanel = new StakeNumPanel({
            "app"  : _this.app,
            "class": "InspectionModule"
        });

        _this.stakeNumPanel.on("stakeNumSelect", function (stakeNum) {
            var targetTime = _this.getStakeNumberTime(stakeNum);
            if (targetTime) {
                _this.sampledPositionViewport.clock.currentTime = targetTime;
            }

        });

        _this.app.ui.layout.bottom.append(_this.stakeNumPanel);
        _this.panels.push(_this.stakeNumPanel);

        _this.sampledPositionViewport = new SampledPositionViewport({
            "app"      : _this.app,
            "direction": _this.globeButtonPanel.directSelect.value,
            "height"   : _this.globeButtonPanel.heightSelect.value,
            "speed"    : _this.globeButtonPanel.speedSelect.value
        });

        _this.sampledPositionViewport.on("toggle", function () {
            _this.playPanel.playButton.toggleClass("pause", function () {
                return _this.sampledPositionViewport.isPlaying;
            });
        });

        _this.sampledPositionViewport.on("tick", function () {

            if (!_this.currentStakeNum || (_this.currentStakeNum.distance !== _this.stakeNumPanel.viewportStakeNum.distance)) {
                _this.computerItemPop(
                    _this.stakeNumPanel.viewportStakeNum,
                    function () {
                        _this.sampledPositionViewport.pause();
                        _this.currentStakeNum = _this.stakeNumPanel.viewportStakeNum;
                    },
                    function () {
                        _this.sampledPositionViewport.play();
                    }
                );
            }


            // console.log(_this.stakeNumPanel.viewportStakeNum.distance);

            // var near = _this.sampledPositionViewport.direction * -5;
            // var far = _this.sampledPositionViewport.direction * -10;
            //
            // var min = Math.min(near, far);
            // var max = Math.max(near, far);
            //
            // $.each(_this.app.modules.road.modules, function (i, module) {
            //     if (module.isOpened) {
            //         $.each(module.items, function (j, item) {
            //
            //             if (item.stakeNum) {
            //
            //                 if (
            //                     item.stakeNum.distance - _this.stakeNumPanel.viewportStakeNum.distance < max
            //                     && item.stakeNum.distance - _this.stakeNumPanel.viewportStakeNum.distance > min
            //                 ) {
            //
            //                     if (item.openPop && !item.popOpend) {
            //
            //                         // console.log(item.stakeNum.distance - _this.stakeNumPanel.viewportStakeNum.distance);
            //                         // console.log(max);
            //                         // console.log(min);
            //
            //                         item.popOpend = true;
            //                         _this.sampledPositionViewport.pause();
            //
            //                         item.openPop && item.openPop();
            //                         item.active && item.active();
            //
            //                         _this.sampledPositionViewport.one("play", function () {
            //                             item.closePop && item.closePop();
            //                             item.active && item.active(false);
            //                         });
            //                     }
            //
            //                 } else if (item.popOpend) {
            //                     // console.log(item.stakeNum.distance - _this.stakeNumPanel.viewportStakeNum.distance);
            //                     item.popOpend = undefined;
            //                 }
            //             }
            //         });
            //     }
            // });

        });

    }

    var _class = InspectionModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "open"              : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {
                    _this.isOpened = true;

                    _this.globeButtonPanel.open(false);
                    _this.playPanel.open(false);
                    _this.stakeNumPanel.open(false);

                    _this.app.modules.road.leftPanel.addClass("withStakeNumPanelBottom");

                    if (!_this.app.modules.road.leftPanel.isOpened) {
                        _this.app.modules.road.leftPanel.open(false);
                        _this.app.modules.road.leftPanel.min(false);
                    }

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"          : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.app.modules.road.stakeNumModule.loadInit(function () {
                        _this.app.modules.road.roadLineModule.loadInit(function () {

                            _this.stakeNumPanel._startStakeNum = _this.app.modules.road.stakeNumModule.startStakeNum;
                            _this.stakeNumPanel._stopStakeNum = _this.app.modules.road.stakeNumModule.stopStakeNum;
                            _this.stakeNumPanel.update();

                            _this.sampledPositionViewport.sampledPosition = _this.app.modules.road.roadLineModule.getSampledRoadCenter();
                            _this.sampledPositionViewport.updateCameraNow();

                            $.each(_this.app.modules.road.modules, function (i, module) {
                                module.on("toggle", function () {
                                    if (_this.isOpened) {
                                        _this.updateModuleItems();
                                    }
                                });
                            });

                            _this.isLoaded = true;

                            _this.trigger("loadInit");
                            callback && callback();

                        });

                    });


                } else {
                    callback && callback();
                }

            }
        },
        "getStakeNumberTime": {
            value: function (stakeNum) {
                var _this = this;

                stakeNum = new Mileage(stakeNum);

                var distance = stakeNum.distance - _this.stakeNumPanel.startStakeNum.distance;
                var stakeNumLength = _this.stakeNumPanel.stopStakeNum.distance - _this.stakeNumPanel.startStakeNum.distance;

                if (distance > 0 && distance < stakeNumLength) {
                    var differSeconds = uEarth.JulianDate.secondsDifference(_this.sampledPositionViewport.clock.stopTime, _this.sampledPositionViewport.clock.startTime);
                    return uEarth.JulianDate.addSeconds(_this.sampledPositionViewport.clock.startTime, distance / stakeNumLength * differSeconds, new uEarth.JulianDate());
                }

            }
        },
        "updateModuleItems" : {
            value: function () {
                var _this = this;

                _this.items = {};

                $.each(_this.app.modules.road.modules, function (i, module) {
                    if (module.isOpened) {
                        $.each(module.items, function (j, item) {

                            if (item.stakeNum && item.openPop) {
                                _this.items[item.stakeNum.distance] = _this.items[item.stakeNum.distance] || [];
                                _this.items[item.stakeNum.distance].push(item);

                            }
                        });
                    }
                });

                console.log(_this.items);

            }
        },
        "computerItemPop"   : {
            value: function (stakeNum, onstart, onfinish) {
                var _this = this;

                if (_this.items[stakeNum.distance]) {
                    _this.needPop = [];
                    onstart && onstart();

                    $.each(_this.items[stakeNum.distance], function (i, item) {
                        _this.needPop.push(item);
                    });
                    _this.popItem(0, onfinish);
                }
            }
        },
        "popItem"           : {
            value: function (index, onfinish) {
                var _this = this;

                if (_this.needPop[index]) {
                    var item = _this.needPop[index];
                    item.openPop && item.openPop();

                    item.one("closePop", function () {
                        _this.popItem(index + 1, onfinish);
                    });

                } else {
                    onfinish && onfinish();
                }


            }
        },
        "toggle"            : {
            value: function () {
                var _this = this;
                if (_this.isOpened) {
                    _this.close();
                } else {
                    _this.open();
                }
            }
        },
        "close"             : {
            value: function () {
                var _this = this;
                _this.closePanel();
                // $.each(_this.items, function (i, item) {
                //     item.close();
                // });
                _this.app.modules.road.leftPanel.close(false);
                _this.app.modules.road.leftPanel.removeClass("withStakeNumPanelBottom");
                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "openPanel"         : {
            value: function () {
                var _this = this;
                $.each(_this.panels, function (i, panel) {
                    panel.open(false);
                });
            }
        },
        "closePanel"        : {
            value: function () {
                var _this = this;

                _this.closeAllPanel();
                _this.isPanelOpened = false;
                _this.trigger("closePanel");
            }
        }
    });

    return _class;

});